package com.gzhryc.weixin.api.event;

import java.io.InputStream;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;

import com.gzhryc.common.xml.XmlTools;
import com.gzhryc.weixin.api.event.enums.EventTypeEnum;
import com.gzhryc.weixin.api.event.enums.MsgTypeEnum;
import com.gzhryc.weixin.api.event.msg.EventObject;
import com.gzhryc.weixin.api.event.msg.req.DefaultEventEntity;
import com.gzhryc.weixin.api.event.msg.req.ImageEventMessage;
import com.gzhryc.weixin.api.event.msg.req.LinkEventMessage;
import com.gzhryc.weixin.api.event.msg.req.LocationEventEntity;
import com.gzhryc.weixin.api.event.msg.req.LocationEventMessage;
import com.gzhryc.weixin.api.event.msg.req.MenuScancodeEventEntity;
import com.gzhryc.weixin.api.event.msg.req.ShortvideoEventMessage;
import com.gzhryc.weixin.api.event.msg.req.SubscribeEventEntity;
import com.gzhryc.weixin.api.event.msg.req.TemplateSendJobFinishEvent;
import com.gzhryc.weixin.api.event.msg.req.TextEventMessage;
import com.gzhryc.weixin.api.event.msg.req.VideoEventMessage;
import com.gzhryc.weixin.api.event.msg.req.VoiceEventMessage;
import com.gzhryc.weixin.gzh.enums.EMenuEventType;

/**
 * 微信事件接收服务
 * @author 张浩
 * @version 0.3.4
 */
public class WxEventReceiveService {
	
	static Logger log = Logger.getLogger(WxEventReceiveService.class);
	
	/** 交互事件处理 */
	static IInteractiveEventService interactiveEventSer;
	/** 通用事件处理 */
	static IEventService eventSer;
	/** 菜单事件处理 */
	static IMenuEventService menuEventSer;

	public static Object receive(InputStream in,Map<String,Object> data){
		try{
			Document doc = XmlTools.buildDocument(in);
			
			EventObject entity = XmlTools.changToObject(doc,EventObject.class, false);
			
			log.info("接收消息：" + XmlTools.changToXmlStr(entity, false));
			
			if(MsgTypeEnum.event.name().equals(entity.getMsgType())){
				//菜单及通用事件
				if(eventSer != null){
					if(EventTypeEnum.subscribe.name().equals(entity.getEvent())){
						return eventSer.subscribe(XmlTools.changToObject(doc,SubscribeEventEntity.class, false),data);
					}else if(EventTypeEnum.unsubscribe.name().equals(entity.getEvent())){
						return eventSer.unsubscribe(XmlTools.changToObject(doc,EventObject.class, false),data);
					}else if(EventTypeEnum.SCAN.name().equals(entity.getEvent())){
						return eventSer.scan(XmlTools.changToObject(doc,SubscribeEventEntity.class, false),data);
					}else if(EventTypeEnum.LOCATION.name().equals(entity.getEvent())){
						return eventSer.location(XmlTools.changToObject(doc,LocationEventEntity.class, false),data);
					}else if(EventTypeEnum.TEMPLATESENDJOBFINISH.name().equals(entity.getEvent())){
						return eventSer.templateSendJobFinish(XmlTools.changToObject(doc,TemplateSendJobFinishEvent.class, false),data);
					}
				}else if(menuEventSer != null){
					if(EMenuEventType.CLICK.name().equals(entity.getEvent())){
						return menuEventSer.click(XmlTools.changToObject(doc,DefaultEventEntity.class, false),data);
					}else if(EMenuEventType.VIEW.name().equals(entity.getEvent())){
						return menuEventSer.view(XmlTools.changToObject(doc,DefaultEventEntity.class, false),data);
					}else if(EMenuEventType.scancode_push.name().equals(entity.getEvent())){
						return menuEventSer.scancodePush(XmlTools.changToObject(doc,MenuScancodeEventEntity.class, false),data);
					}else if(EMenuEventType.scancode_waitmsg.name().equals(entity.getEvent())){
						return menuEventSer.scancodeWaitmsg(XmlTools.changToObject(doc,MenuScancodeEventEntity.class, false),data);
					}
				}
			}else if(interactiveEventSer != null){
				if(MsgTypeEnum.voice.name().equals(entity.getMsgType())){
					return interactiveEventSer.voice(XmlTools.changToObject(doc,VoiceEventMessage.class, false),data);
				}else if(MsgTypeEnum.text.name().equals(entity.getMsgType())){
					return interactiveEventSer.text(XmlTools.changToObject(doc,TextEventMessage.class, false),data);
				}else if(MsgTypeEnum.image.name().equals(entity.getMsgType())){
					return interactiveEventSer.image(XmlTools.changToObject(doc,ImageEventMessage.class, false),data);
				}else if(MsgTypeEnum.link.name().equals(entity.getMsgType())){
					return interactiveEventSer.link(XmlTools.changToObject(doc,LinkEventMessage.class, false),data);
				}else if(MsgTypeEnum.location.name().equals(entity.getMsgType())){
					return interactiveEventSer.location(XmlTools.changToObject(doc,LocationEventMessage.class, false),data);
				}else if(MsgTypeEnum.shortvideo.name().equals(entity.getMsgType())){
					return interactiveEventSer.shortvideo(XmlTools.changToObject(doc,ShortvideoEventMessage.class, false),data);
				}else if(MsgTypeEnum.video.name().equals(entity.getMsgType())){
					return interactiveEventSer.video(XmlTools.changToObject(doc,VideoEventMessage.class, false),data);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	public static void setInteractiveEventSer(IInteractiveEventService interactiveEventSer) {
		WxEventReceiveService.interactiveEventSer = interactiveEventSer;
	}

	public static void setEventSer(IEventService eventSer) {
		WxEventReceiveService.eventSer = eventSer;
	}

	public static void setMenuEventSer(IMenuEventService menuEventSer) {
		WxEventReceiveService.menuEventSer = menuEventSer;
	}
}
